import mongoose from './dbase';

type ALL_MODULES_MODEL = {
  [K in keyof SchemaModules]: mongoose.Model<
    mongoose.Document & SchemaModules[K],
    (typeof schemaModules)[K]
  >;
};
var Schema = mongoose.Schema;

const schemaModules: SchemaModulesObj = {
  User: {
    username: String,
    password: String,
    sex: String
  },
  Resource: {
    type: {
      type: String,
      index: true
    },
    category: {
      type: String,
      index: true
    },
    target_url: {
      type: String,
      index: true,
      unique: true
    },
    url: {
      type: String,
      index: true,
      unique: true
    },
    is_down: Boolean,
    fail_number: Number,
    down_time: Number,
    parent_id: {
      type: String,
      index: true
    },
    loading: Boolean,
    index: Number,
    name: String
  },
  ImageGroup: {
    type: {
      type: String,
      index: true
    },
    category: {
      type: String,
      index: true
    },
    target_url: {
      type: String,
      index: true,
      unique: true
    },
    url: {
      type: String,
      index: true,
      unique: true
    },
    is_down: Boolean,
    fail_number: Number,
    down_time: Number,
    parent_id: String,
    loading: Boolean,
    index: Number,
    name: String,
    images: Array
  }
};

export let DB = Object.keys(schemaModules).reduce(
  (result, key) => {
    result[key] = mongoose.model(key, new Schema(schemaModules[key]));
    return result;
  },
  {} as ALL_MODULES_MODEL
);

export default DB;
